package TestExercises06;

//IO流

/*
制造假数据:
    利用糊涂包（Hutool）生成假数据，并写到文件当中
 */

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;

import java.util.*;

public class Test02 {
    public static void main(String[] args) {

        //1、定义变量记录网址
        String famialNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";

        //2、爬取数据
        String famialNameStr = HttpUtil.get(famialNameNet);
        String boyNameStr = HttpUtil.get(boyNameNet);
        String girlNameStr = HttpUtil.get(girlNameNet);

        //3.利用正则表达式获取数据
        //通过正则表达式，把其中符合要求的数据获取出来
        List<String> famialyNameTempList = ReUtil.findAll("(.{4})(，|。)", famialNameStr, 1);
        List<String> boyNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、|。)", boyNameStr, 1);
        List<String> girlNameTempList = ReUtil.findAll("(.. ){4}..", girlNameStr, 0);

        //4、处理数据
        //famialyNameTempList（姓氏）
        ArrayList<String> famialyNameList = new ArrayList<>();
        for (String str : famialyNameTempList) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                famialyNameList.add(c + "");
            }
        }

        //boyNameTempList（男生姓名）
        ArrayList<String> boyNameList = new ArrayList<>();
        for (String str : boyNameTempList) {
            if (!boyNameList.contains(str)) {
                boyNameList.add(str);
            }
        }

        //girlNameTempList（女生姓名）
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String str : girlNameTempList) {
            String[] arr = str.split(" ");
            for (int i = 0; i < arr.length; i++) {
                if (!girlNameList.contains(arr[i])) {
                    girlNameList.add(arr[i]);
                }
            }
        }

        //5、生成数据  姓名-性别-年龄
        ArrayList<String> list = getInfos(famialyNameList, boyNameList, girlNameList, 70, 50);
        Collections.shuffle(list);

        //6、写出数据
        //细节:糊涂包的相对路径，不是相对于当前项目而言的，而是相对class文件而言的
        FileUtil.writeLines(list, "D:\\工作\\工作\\src\\names2.txt", "UTF-8");


    }

    public static ArrayList<String> getInfos(ArrayList<String> famialyNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList, int boyCount, int girlCount) {

        //1、生成男生的姓名
        HashSet<String> boyhs = new HashSet<>();
        while (true) {
            if (boyhs.size() == boyCount) {
                break;
            }
            Collections.shuffle(famialyNameList);
            Collections.shuffle(boyNameList);

            boyhs.add(famialyNameList.get(0) + boyNameList.get(0));
        }

        //2、生成女生的姓名
        HashSet<String> girlhs = new HashSet<>();
        while (true) {
            if (girlhs.size() == girlCount) {
                break;
            }
            Collections.shuffle(famialyNameList);
            Collections.shuffle(girlNameList);

            girlhs.add(famialyNameList.get(0) + girlNameList.get(0));
        }

        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        //3、生成男生信息
        for (String boyName : boyhs) {
            int age = r.nextInt(10) + 18;//18岁~27岁
            list.add(boyName + "-男-" + age);
        }

        //4、生成女生信息
        for (String girlName : girlhs) {
            int age = r.nextInt(8) + 18;//18岁~25岁
            list.add(girlName + "-女-" + age);
        }

        return list;
    }
}
